package apiday07.thread;

public class SyncApiDemo1 {
    public static void main(String[] args) {
//        StringBuilder sb = new StringBuilder();//线程不安全
        StringBuffer sb = new StringBuffer();//线程安全
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    sb.append("hello");
                }
            }
        };

        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    sb.append("world");
                }
            }
        };

        t1.start();
        t2.start();

        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(sb.length());

    }
}
